//
//  XLEmotionKeyBoard.swift
//  XLVeibo
//
//  Created by macbook on 16/7/4.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

class XLEmotionKeyBoard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
    backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(tools)
        addSubview(pageView)
        addSubview(pageController)
        
        //添加约束
        tools.snp_makeConstraints { (make) in
            
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(37)
        }
        
        pageView.snp_makeConstraints { (make) in
            
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(tools.snp_top)
        }
        
        pageController.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(pageView)
            make.centerX.equalTo(self)
        }
        
        
        //闭包的实现，确定点击那个按钮
        tools.toolbarClosure = {(type:XLEmotionToolsType)->() in
            
            //获取indexpath
            let indexpath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            //滚动到指定位置
            self.pageView.scrollToItemAtIndexPath(indexpath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
//            //总页数
            self.pageController.numberOfPages = XLEmoticonShardTools.shardTools.allEmotion[indexpath.section].count
            //当前页
            self.pageController.currentPage = 0
            
        }
    }
    
    //懒加载tools
    private lazy var tools:XLEmoticonTools = XLEmoticonTools()
   
    //page
    private lazy var pageView:XLEmoticonPageView = {
    
        let page = XLEmoticonPageView()
        page.bounces = false
        page.pagingEnabled = true
        
        page.showsVerticalScrollIndicator = false
        page.showsHorizontalScrollIndicator = false
        page.delegate = self
        
        return page
    }()
    
    //分页指示器
    private lazy var pageController:UIPageControl = {
    
        let view = UIPageControl()
        //总页数
        view.numberOfPages = XLEmoticonShardTools.shardTools.allEmotion[0].count
        //当前页数
        view.currentPage = 0
        //通过kvc赋值
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
    }()
}


extension XLEmotionKeyBoard:UICollectionViewDelegate{

    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //获取相对于屏幕中心点x
        let centerX = KScreenW / 2 + scrollView.contentOffset.x
        
        //获取屏幕显示cell
        let cellArray = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
           return cell1.frame.origin.x < cell2.frame.origin.x
            
        }
        
        //如果当前屏幕显示两个cell我们才去判断
        if cellArray.count == 2 {
            
            //第一个cell
            let firstCell = cellArray.first!
            //第二个cell
            let lastCell = cellArray.last!
            
            //获取当前显示多的cell的indexPath
            var indexPath:NSIndexPath
            
            //判断我们的中心点是否在第一个cell中，代表第一个cell显示的多
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                
                indexPath = pageView.indexPathForCell(firstCell)!
            }else{
            
                //最后一个显示的多
                indexPath = pageView.indexPathForCell(lastCell)!
            
            }
            
            //滚动pageView 改变toolbar 的选中状态
            tools.setCurrentButtons(indexPath.section)
            
            
            //总页数
            pageController.numberOfPages = XLEmoticonShardTools.shardTools.allEmotion[indexPath.section].count
            //当前页
            pageController.currentPage = indexPath.item
        }
    }
}





